package com.minelx.scriptbuilder.script.attribute.value;

import java.util.function.BinaryOperator;

import static java.lang.Integer.parseInt;

public class Operator implements IAttribute {

	private final IAttribute left;

	private final IAttribute right;

	private final BinaryOperator<String> operatorAsFunction;

	private Operator(IAttribute left, IAttribute right, BinaryOperator<String> operatorAsFunction) {
		this.left = left;
		this.right = right;
		this.operatorAsFunction = operatorAsFunction;
	}

	@Override
	public String asString() {
		return operatorAsFunction.apply(left.asString(), right.asString());
	}

	public static Operator multi(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(parseInt(l) * parseInt(r)));
	}

	public static Operator reduce(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(parseInt(l) - parseInt(r)));
	}

	public static Operator plus(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(parseInt(l) + parseInt(r)));
	}

	public static Operator divide(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(parseInt(l) / parseInt(r)));
	}

	public static Operator or(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(Boolean.parseBoolean(l) || Boolean.parseBoolean(r)));
	}

	public static Operator and(IAttribute left, IAttribute right) {
		return new Operator(left, right, (l, r) -> String.valueOf(Boolean.parseBoolean(l) && Boolean.parseBoolean(r)));
	}
}
